package parser

import (
	"fmt"
	"github.com/hashicorp/hcl/v2"
	"github.com/ihaiker/tfwrapper/internal/config"
	"github.com/ihaiker/tfwrapper/internal/errors"
	"github.com/ihaiker/tfwrapper/internal/util"
)

func versionConstraintCheck(running *config.Running) (diags hcl.Diagnostics) {
	if running.TerraformVersion, diags = getTerraformVersion(running.TerraformBinary); util.HasErrors(diags) {
		return
	}

	if e := checkVersionMeetsConstraint("terraform", running.TerraformVersion, running.TerraformVersionConstraint); e != nil {
		diags = errors.Diagnostics("Invalid version",
			fmt.Errorf("terraform need %s but %s", *running.TerraformVersionConstraint, running.TerraformVersion))
		return
	}
	if e := checkVersionMeetsConstraint("terraform", running.TerraformVersion, running.TerraformVersionConstraint); e != nil {
		diags = errors.Diagnostics("Invalid version",
			fmt.Errorf("tfwrapper need %s but %s", *running.TFWrapperVersionConstraint, running.TFWrapperVersion))
		return
	}

	return
}
